<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">
				<text v-if="type == '1'">添加支付宝</text>
				<text v-if="type == '4'">银联卡</text>
				<text v-if="type == '2'">微信</text>
			</block>
		</cu-custom>
		<bank v-if="type == '4'" :BankInfo="BankInfo"></bank>
		<wechat v-if="type == '2'" :BankInfo="BankInfo"></wechat>
		<alipay v-if="type == '1'" :BankInfo="BankInfo"></alipay>
		<view class="margin">
			<view class="cu-btn lg mt30 bg-main fw600" style="width: 100%;" v-on:tap="save">保存</view>
			<view class="cu-btn bg-white lg mt20" style="width: 100%;" v-if="BankInfo.ID > 0" v-on:tap="del">删除</view>
		</view>
	</view>
</template>

<script>
import base from 'utils/base.js';
import tools from 'utils/tools.js';
import Bank from '@/pages/my/setting/bankComponents/bank';
import Alipay from '@/pages/my/setting/bankComponents/alipay';
import Wechat from '@/pages/my/setting/bankComponents/wechat';
let _self;
export default {
	components: {
		Alipay,
		Bank,
		Wechat
	},
	data() {
		return {
			type: '1',
			act: '',
			id: 0,
			BankInfo: { Name: '', Phone: '', BankNo: '', Type: 1, ID: 0, BankName: '支付宝' }
		};
	},
	onLoad(options) {
		_self = this;
		_self.id = options.id;
		_self.type = options.type || '1';
		_self.act = options.act || 'add';
		_self.BankInfo.Type = options.type;
	},
	methods: {
		del: function() {
			let currentPage = this;
			uni.showModal({
				confirmColor: '#ff5e1f',
				title: '提示',
				content: '确定要删除吗',
				success: function(res) {
					if (res.confirm) {
						let params = {
							type: 'GET',
							url: '/api/Withdrawals/DeleteUserBank',
							data: { id: currentPage.BankInfo.ID },
							sCallback: function(response) {
								if (response.IsSuccess) {
									uni.navigateBack({
										delta: 1
									});
								}
							}
						};
						base.request(params);
					}
				}
			});
		},
		save: function() {
			let currentPage = this;
			var bank = currentPage.BankInfo;
			if (!tools.IsChineseName(bank.Name)) {
				tools.showToast('请准确填写中文姓名');
				return;
			}
			if (bank.BankNo.trim() == '') {
				tools.showToast('请准确填写账号');
				return;
			}
			if (bank.Phone.trim() == '' && bank.Type <=2) {
				tools.showToast('请准确填写手机号码');
				return;
			}
			if (tools.IsPhone(bank.Phone) == false && bank.Type <=2) {
				tools.showToast('手机号码格式不正确');
				return;
			}
			if (tools.IsBankCard(bank.BankNo) == false && bank.Type ==4) {
				tools.showToast('银行卡号格式不正确');
				return;
			}
			if (bank.Type == 2) bank.BankName = '微信钱包';
			let requestURL = '/api/Withdrawals/UpdateUserBank';
			if (bank.ID == 0) requestURL = '/api/Withdrawals/AddUserBank';
			let params = {
				type: 'POST',
				url: requestURL,
				data: bank,
				sCallback: function(response) {
					if (response.IsSuccess) {
						uni.navigateBack({
							delta: 1
						});
					}
				}
			};
			base.request(params);
		},
		GetBank: function() {
			let currentPage = this;
			if (currentPage.id > 0) {
				let params = {
					hideLoading: true,
					type: 'GET',
					url: '/api/Withdrawals/GetUserBankInfo',
					data: { id: currentPage.id },
					sCallback: function(response) {
						if (response.IsSuccess) {
							let bankInfo = response.Data;
							currentPage.BankInfo = bankInfo;
						}
					}
				};
				base.request(params);
			}
		}
	},
	mounted() {
		this.GetBank();
	}
};
</script>

<style lang="scss" scoped>
.cu-btn {
	border-radius: 80upx;
	font-size: 28upx;
}
</style>
